package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.MyListViewItemBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/21.
 */
public class MyAdapter extends BaseAdapter {
    Context context;
    List<MyListViewItemBean> data;
    LayoutInflater inflater;

    public MyAdapter(Context context, List<MyListViewItemBean> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holledler holledler = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_my_item, null);
            holledler = new Holledler(view);
            view.setTag(holledler);
        } else {
            holledler = (Holledler) view.getTag();
        }
        MyListViewItemBean bean;
        try {
            bean = (MyListViewItemBean) getItem(i);
            SetTypeface.set(holledler.icon, bean.getIcon());
            holledler.title.setText(bean.getName());
            if (bean.isHasNew()) {
                holledler.point.setVisibility(View.VISIBLE);
            } else {
                holledler.point.setVisibility(View.GONE);
            }
            holledler.item.setOnClickListener(bean.getListener());

        } catch (Exception e) {
        }
        return view;
    }

    class Holledler {
        RelativeLayout item;
        TextView icon, title, point, right;

        Holledler(View v) {

            item = (RelativeLayout) v.findViewById(R.id.my_item);
            icon = (TextView) v.findViewById(R.id.my_icon);
            title = (TextView) v.findViewById(R.id.my_title);
            point = (TextView) v.findViewById(R.id.my_point);
            right = (TextView) v.findViewById(R.id.my_right);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }
}
